package com.dgut.lotterySystem.common.pojo;

import com.dgut.lotterySystem.common.errorcode.ErrorCode;
import com.dgut.lotterySystem.common.errorcode.GlobalErrorCodeConstants;
import lombok.Data;
import org.springframework.util.Assert;

import java.io.Serializable;

@Data
public class CommonResult<T> implements Serializable {
    /**
     * 错误码
     */
    private Integer code;
    /**
     * 错误描述信息
     */
    private String msg;
    /**
     * 正确返回数据
     */
    private T data;

    public static <T> CommonResult<T> success(T data){
        CommonResult<T> commonResult=new CommonResult<>();
        commonResult.code= GlobalErrorCodeConstants.SUCCESS.getCode();
        commonResult.msg="";
        commonResult.data=data;
        return commonResult;
    }

    public static <T> CommonResult<T> error(Integer code,String message){
        Assert.isTrue(!GlobalErrorCodeConstants.SUCCESS.getCode().equals(code),
                "当前错误的错误码是成功码");
        CommonResult<T> commonResult=new CommonResult<>();
        commonResult.code= GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode();
        commonResult.msg=message;
        return commonResult;
    }


    public static <T> CommonResult<T> error(ErrorCode errorCode){
        return error(errorCode.getCode(), errorCode.getMsg());
    }

}
